local M = {}

function M.touchDelegate(params)
	local target = params.target
	local onMove = params.onMove or function() end

	local onRelease = params.onRelease or function() end

	local lastFrameX, lastFrameY = 0, 0
	local beginX, beginY = target.x, target.y
	local function ontouch(event)
		if event.phase == "began" then
			if not event.target.isFocus then
				display.getCurrentStage():setFocus(event.target)
				event.target.isFocus = true
				lastFrameX, lastFrameY = event.x, event.y
			else
				-- onMove({frameOffsetX = event.x - lastFrameX, frameOffsetY = event.y - lastFrameY})
				-- lastFrameX, lastFrameY = event.x, event.y
			end
		elseif event.phase == "moved" then
			if event.target.isFocus then
				onMove({
					frameOffsetX = event.x - lastFrameX,
					frameOffsetY = event.y - lastFrameY,
					offsetX = event.x - beginX,
					offsetY = event.y - beginY,
				})
				lastFrameX, lastFrameY = event.x, event.y
			end
		elseif event.phase == "ended" or event.phase == "cancelled" then
			if event.target.isFocus then
				display.getCurrentStage():setFocus(nil)
				event.target.isFocus = false
			end
			if event.phase == "ended" then
				onRelease(event)
			end
		end

		return true
	end

	target:addEventListener("touch", ontouch)

	target:addEventListener("tap", function() return true  end)
end

function M.mouseDelegate(params)
	local target = params.target

	local lastFrameX, lastFrameY = 0, 0

	--local onHover = params.onHover or function() end
	--local onCancelHover = function(event)
	--	if params.onCancelHover then
	--		params.onCancelHover(event)
	--		event.target.isHover = false
	--	end
	--end



	--local function hoverEvent(event)
	--	if (event.isPrimaryButtonDown) or (event.isSecondaryButtonDown) or (event.isMiddleButtonDown) then
	--		if event.target.isHover then
	--			onCancelHover(event)
	--			event.target.isHover = false
	--		end
	--		return
	--	end
	--
	--	if not uiUtils.isInBounds(event.target, event.x, event.y) then
	--		if event.target.isHover then
	--			onCancelHover(event)
	--			event.target.isHover = false
	--		end
	--		return
	--	end
	--
	--	if not event.target.isHover then
	--		event.target.isHover = true
	--		eventDispatcher:dispatchEvent("mouseOnNewObject", {target = event.target})
	--	end
	--
	--	onHover(event)
	--end

	local onMid = params.onMid or function() end
	local onCancelMid = function(event)
		event.target.isMid = false
		display.getCurrentStage():setFocus(nil)

		if params.onCancelMid then
			params.onCancelMid(event)
		end
	end

	local function midPressEvent(event)
		if not event.isMiddleButtonDown then
			if event.target.isMid then
				onCancelMid(event)
			end
			return
		end

		if not event.target.isMid then
			event.target.isMid = true
			display.getCurrentStage():setFocus(event.target)
		end
		onMid(event)
	end

	--target:addEventListener("mouse", hoverEvent)
	--target:addEventListener("mouse", mainPressEvent)
	target:addEventListener("mouse", midPressEvent)


	--local function onMouseNewObj(event)
	--	if not (event.target == params.target) then
	--		onCancelHover(event)
	--		onCancelMid(event)
	--	end
	--end
	--eventDispatcher:addEventListener(
	--	"mouseOnNewObject",
	--	onMouseNewObj,
	--	function()
	--		return not coreDisplay.isDisplayObj(target)
	--	end
	--)
end

-- todo 这边没有做深拷贝等问题的考虑
local function packInputParams(params)
	local t = {
		x = 0, y = 0,
		width = 90, height = 25,
		inputType = "number",
		placeholder = "",
		onSubmit = nil,
	}
	tableUtils.setMeta(params, t)
end

function M.createInputField(params)
	params = params or {}
	packInputParams(params)

	local gInput = display.newGroup()

	local gInputFiled = native.newTextField(params.x, params.y, params.width, params.height)
	gInput:insert(gInputFiled)

	gInputFiled.inputType = params.inputType or "default"
	gInputFiled.placeholder = tostring(params.placeholder or "")

	gInputFiled:addEventListener("userInput", function(event)
		if event.phase == "submitted" then
			if params.onSubmit then
				params.onSubmit(event)
			end
		end
	end)

	function gInput:setText(str)
		str = tostring(str)
		gInputFiled.text = str
	end

	return gInput
end
return M